#include <QApplication>
#include <QGraphicsView>
#include <QTimer>
#include "common/common.h"
#include "database/connection.h"
#include "widget/loading.h"
#include "widget/account.h"
#include "widget/login.h"
#include "widget/begin.h"
#include "widget/param.h"
#include "widget/music.h"
#include "widget/pause.h"
#include "widget/save.h"
#include "widget/rank.h"
#include "widget/pause.h"
#include "widget/save.h"
#include "common/statusvalue.h"
#include "game/myview.h"
#include "game/myscene.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Loading *loading = new Loading();
    loading->show();

    //创建和打开数据库链接
    if ( !createConnection())
        return 1;

    Login *login = new Login();
    login->hide();

    Account *account = new Account();
    account->hide();

    Begin *begin = new Begin();
    begin->hide();

    Param *param = new Param();
    param->hide();

    Music *music = new Music();
    music->hide();

    Rank *rank = new Rank();
    rank->hide();

    //场景
    MyScene *scene = new MyScene;

    //视图
    MyView *view = new MyView;
    view->setScene(scene);
    view->hide();

    Pause *pausing = new Pause();
    pausing->hide();

    Save *save = new Save;
    save->hide();

    QObject::connect( save, SIGNAL(enterBegin()), begin, SLOT(show()));

    QObject::connect( scene, SIGNAL(over()), save, SLOT(saveShow()));
    QObject::connect( scene, SIGNAL(over()), view, SLOT(hide()));
    QObject::connect( scene, SIGNAL(pause()), pausing, SLOT(show()));
    QObject::connect( scene, SIGNAL(pause()), view, SLOT(hide()));

    QObject::connect( pausing, SIGNAL(backGame()), scene, SLOT(continueGame()));
    QObject::connect( pausing, SIGNAL(backGame()), view, SLOT(show()));
    QObject::connect( pausing, SIGNAL(enterMusic()), music, SLOT(musicShow()));
    QObject::connect( pausing, SIGNAL(enterRank()), rank, SLOT(rankShow()));
    QObject::connect( pausing, SIGNAL(enterSave()), save, SLOT(saveShow()));

    QObject::connect( begin, SIGNAL(enterGame()), scene, SLOT(beginGame()));
    QObject::connect( begin, SIGNAL(enterGame()), view, SLOT(show()));
    QObject::connect( begin, SIGNAL(enterParam()), param, SLOT(showParam()));
    QObject::connect( begin, SIGNAL(enterMusic()), music, SLOT(musicShow()));
    QObject::connect( begin, SIGNAL(enterRank()), rank, SLOT(rankShow()));

    QObject::connect( login, SIGNAL(enterGame()), begin, SLOT(show()));
    QObject::connect( login, SIGNAL(createNewPlayer(QString)), account, SLOT(accountShow(QString)));

    QObject::connect( account, SIGNAL(cancel()), login, SLOT(loginShow()));

    QObject::connect( param, SIGNAL(enterBegin()), begin, SLOT(show()));

    QObject::connect( music, SIGNAL(enterPause()), pausing, SLOT(show()));
    QObject::connect( music, SIGNAL(enterBegin()), begin, SLOT(show()));

    QObject::connect( rank, SIGNAL(enterPause()), pausing, SLOT(show()));
    QObject::connect( rank, SIGNAL(enterBegin()), begin, SLOT(show()));

    // 2秒单触发定时器
    QTimer *time = new QTimer;
    time->singleShot( 2500, loading, SIGNAL(over()));
    QObject::connect( loading, SIGNAL(over()), login, SLOT(loginShow()));
    StatusValue::status = Ready;

    return a.exec();
}
